﻿using Abp.Linq.Expressions;
using Entity.Models.dto;
using Entity.Models.Entities;
using Entity.Models.vo;
using IBLL;
using IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using utils.enums;
using utils.Enums;
using utils.EX;
using Utils;

namespace BLL
{
    /// <summary>
    /// 部门BLL层
    /// </summary>
    public class DepartmentBLL : IDepartmentBLL
    {
        /// <summary>
        /// 部门DAL接口
        /// </summary>
        IDepartmentDAL _departmentDal;

        /// <summary>
        /// 用户接口
        /// </summary>
        IUserDAL _userDAL;

        public DepartmentBLL(IDepartmentDAL department, IUserDAL userDAL)
        {
            _departmentDal = department;
            _userDAL = userDAL;
        }

        /// <summary>
        /// 部门数据列表、搜索
        /// </summary>
        /// <param name="DepartmentName">部门名称</param>
        /// <param name="page">页数</param>
        /// <param name="limit">数据条数</param>
        /// <returns></returns>
        public List<DepartmentDTO> getdepartmentList(string DepartmentName, string page, string limit, out int cont)
        {
            //连表
            var datas = from d in _departmentDal.getEntitys().Where(i => i.IsDelete != true)
                        join u in _userDAL.getUserShowDAL().Where(i => i.IsDelete != true)
                        on d.LeaderId equals u.Id
                        into Temedu
                        from uu in Temedu.DefaultIfEmpty()

                        join d2 in _departmentDal.getEntitys().Where(u => u.IsDelete != true)
                        on d.ParentId equals d2.Id
                        into TempDD
                        from dd2 in TempDD.DefaultIfEmpty()
                        select new DepartmentDTO
                        {
                            Id=d.Id,
                            _createTimes = d.CreateTime,
                            _description = d.Description,
                            _departmentName = d.DepartmentName,
                            _parentName = dd2.DepartmentName,
                            _leaderName = uu.UserName
                        };

            if (!string.IsNullOrEmpty(DepartmentName))
            {
                datas = datas.Where(i => i._departmentName.Contains(DepartmentName));
            }
            //分页
            var pagedepartment = datas.OrderByDescending(d => d._createTimes).Skip((int.Parse(page) - 1) * int.Parse(limit)).Take(int.Parse(limit)).ToList();
            cont = datas.Count();

            //整合数据
            List<DepartmentDTO> departmentList = new List<DepartmentDTO>();
            foreach (var item in pagedepartment)
            {
                departmentList.Add(new DepartmentDTO(item));
            }
            return departmentList;
        }

        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="department"></param>
        /// <returns></returns>。
        public bool AddDepartment(DepartmentInfo department)
        {
            department.Id = Guid.NewGuid().ToString();
            department.CreateTime = DateTime.Now;
            department.ParentId = department.ParentId == null ? "0" : department.ParentId;
            //根据账号查询用户信息
            DepartmentInfo bydepartmentInfo = _departmentDal.getEntitys().FirstOrDefault(d => d.IsDelete != true && d.DepartmentName == department.DepartmentName);
            if (bydepartmentInfo == null)
            {
                //保存
                if (_departmentDal.AddEntity(department) < 0)
                {
                    throw new MyException(((int)UserEnums.USER_STATE.LOSE), Enums.getDescribe(UserEnums.USER_STATE.LOSE));
                }
            }
            else
            {
                throw new MyException(((int)DepartmentEnums.STATE.ALREADY), Enums.getDescribe(DepartmentEnums.STATE.ALREADY));
            }
            return true;
        }

        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="departmentId">部门id</param>
        /// <returns></returns>
        public bool DalateDepartment(string departmentId)
        {
            //删除
            if (_departmentDal.SoftDalateEntity(departmentId) < 0)
            {
                throw new MyException(((int)UserEnums.USER_STATE.DATATE_LOSE), Enums.getDescribe(UserEnums.USER_STATE.DATATE_LOSE));
            }
            return true;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="departmentIds"></param>
        /// <returns></returns>
        public bool DalateDepartmentList(List<String> departmentIds)
        {
            if (departmentIds.Count<1)
            {
                throw new MyException(((int)UserEnums.USER_STATE.UPDATE_LIST), Enums.getDescribe(UserEnums.USER_STATE.UPDATE_LIST));
            }
            foreach (var item in departmentIds)
            {
                DalateDepartment(item);
            }
            return true;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="department"></param>
        /// <returns></returns>
        public bool UpdataDepartment(DepartmentInfo department)
        {
            if (_departmentDal.UpdataEntity(department)<0)
            {
                throw new MyException(((int)UserEnums.USER_STATE.UPDATE_LOSE), Enums.getDescribe(UserEnums.USER_STATE.UPDATE_LOSE));
            }
            return true;
        }

        /// <summary>
        /// 根据部门id搜索
        /// </summary>
        /// <param name="departmentid"></param>
        /// <returns></returns>
        public DepartmentInfo getByDepartment(string departmentid)
        {
            return _departmentDal.getByEntityId(departmentid);
        }

        /// <summary>
        /// 获取全部部门信息
        /// </summary>
        /// <returns></returns>
        public List<DepartmentDTO> getDepartment()
        {
            List<DepartmentInfo> department = _departmentDal.getEntitys().Where(i => i.IsDelete != true).ToList();

            List<DepartmentDTO> departmentDTOs = new List<DepartmentDTO>();
            foreach (var item in department)
            {
                departmentDTOs.Add(new DepartmentDTO(item));
            }
            return departmentDTOs;
        }

        /// <summary>
        /// 部门下拉框
        /// </summary>
        /// <returns></returns>
        public List<DepartmentVO> getDepartmentSelect()
        {
            IQueryable<DepartmentInfo> department = _departmentDal.getEntitys().Where(i => i.IsDelete != true);

            //获取顶级部门
            List<DepartmentVO> departmentlists= department.Where(d => d.ParentId== null).Select(d=>new DepartmentVO(d) ).ToList();

            //查询子部门
            Childrens(departmentlists, department);

            return departmentlists;
        }

        /// <summary>
        /// 查询子部门
        /// </summary>
        /// <param name="departmentlists"></param>
        /// <param name="department"></param>
        private void Childrens(List<DepartmentVO> departmentlists, IQueryable<DepartmentInfo> department)
        {
            //子节点数据集
            List<DepartmentVO> departmentVOs = new List<DepartmentVO>();

            //查询子节点
            foreach (var items in departmentlists)
            {
                List<DepartmentVO> departmentVO=  department.Where(d => d.ParentId.EndsWith(items.id)).Select(d => new DepartmentVO(d)).ToList();
                //递归
                Childrens(departmentVO, department);
                items.children = departmentVO;
            }

        }

    }
}
